package com.ruoyi.project.jiaqin.company.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.mapper.CompanyMapper;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.companyautograph.domain.CompanyAutograph;
import com.ruoyi.project.jiaqin.companyautograph.mapper.CompanyAutographMapper;
import com.ruoyi.project.jiaqin.companycarousel.domain.CompanyCarousel;
import com.ruoyi.project.jiaqin.companycarousel.mapper.CompanyCarouselMapper;
import com.ruoyi.project.jiaqin.companyenvironment.domain.CompanyEnvironment;
import com.ruoyi.project.jiaqin.companyenvironment.mapper.CompanyEnvironmentMapper;
import com.ruoyi.project.jiaqin.companyinfo.domain.CompanyInfo;
import com.ruoyi.project.jiaqin.companyinfo.mapper.CompanyInfoMapper;
import com.ruoyi.project.jiaqin.companyinfo.service.ICompanyInfoService;
import com.ruoyi.project.jiaqin.companynews.domain.CompanyNews;
import com.ruoyi.project.jiaqin.companynews.mapper.CompanyNewsMapper;
import com.ruoyi.project.jiaqin.companyproduct.domain.CompanyProduct;
import com.ruoyi.project.jiaqin.companyproduct.mapper.CompanyProductMapper;
import com.ruoyi.project.jiaqin.companyvideo.domain.CompanyVideo;
import com.ruoyi.project.jiaqin.companyvideo.mapper.CompanyVideoMapper;
import com.ruoyi.project.jiaqin.followcompany.domain.JiaqinFollowCompany;
import com.ruoyi.project.jiaqin.followcompany.service.IJiaqinFollowCompanyService;
import com.ruoyi.project.jiaqin.followmember.domain.JiaqinFollowMember;
import com.ruoyi.project.jiaqin.followmember.service.IJiaqinFollowMemberService;
import com.ruoyi.project.jiaqin.membernewslikes.domain.MemberNewsLikes;
import com.ruoyi.project.jiaqin.membernewslikes.service.IMemberNewsLikesService;
import com.ruoyi.project.jiaqin.news.domain.News;
import com.ruoyi.project.jiaqin.news.mapper.NewsMapper;
import com.ruoyi.project.jiaqin.supply.domain.Supply;
import com.ruoyi.project.jiaqin.supply.mapper.SupplyMapper;
import com.ruoyi.project.jiaqin.workshop.domain.CompanyWorkshop;
import com.ruoyi.project.jiaqin.workshop.mapper.CompanyWorkshopMapper;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 公司Service业务层处理
 *
 * @author admin
 * @date 2021-01-05
 */
@Service("companyService")
public class CompanyServiceImpl implements ICompanyService
{
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private SupplyMapper supplyMapper;
    @Autowired
    private IJiaqinFollowCompanyService iJiaqinFollowCompanyService;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private CompanyInfoMapper companyInfoMapper;
    @Autowired
    private CompanyNewsMapper companyNewsMapper;
    @Autowired
    private CompanyCarouselMapper companyCarouselMapper;
    @Autowired
    private CompanyEnvironmentMapper companyEnvironmentMapper;
    @Autowired
    private CompanyProductMapper companyProductMapper;
    @Autowired
    private CompanyWorkshopMapper companyWorkshopMapper;
    @Autowired
    private CompanyVideoMapper companyVideoMapper;
    @Autowired
    private CompanyAutographMapper companyAutographMapper;
    @Autowired
    private IdGererateFactory idGererateFactory;






    /**
     * 查询公司
     *
     * @param id 公司ID
     * @return 公司
     */
    @Override
    public Company selectCompanyById(String id)
    {
        return companyMapper.selectCompanyById(id);
    }

    /**
     * 查询所有公司ID和name
     * @return
     */
    @Override
    public List<Company> selectAllCompany() {
        return companyMapper.selectAllCompany();
    }


    @Override
    public Company selectCompanyByMemberId(String memberId) {
        return companyMapper.selectCompanyByMemberId(memberId);
    }

    @Override
    public Company selectCompanyBySysUserId(Long sysUserId) {
        return companyMapper.selectCompanyBySysUserId(sysUserId);
    }

    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private IMemberNewsLikesService memberNewsLikesService;
    @Autowired
    private IJiaqinFollowCompanyService jiaqinFollowCompanyService;
    @Autowired
    private IJiaqinFollowMemberService jiaqinFollowMemberService;
    @Autowired
    private ICompanyInfoService companyInfoService;

    @Override
    public JSONObject selectCompanyDetail(String companyId) {
        Company company = companyMapper.selectCompanyById(companyId);
        JSONObject data = new JSONObject();
        CompanyCarousel companyCarousel = new CompanyCarousel();
        companyCarousel.setCompanyId(companyId);
        List<CompanyCarousel> companyCarousels = companyCarouselMapper.selectCompanyCarouselList(companyCarousel);

        CompanyInfo info = companyInfoMapper.selectCompanyInfoByCompanyId(company.getMemberId());
        CompanyVideo companyVideo = companyVideoMapper.selectCompanyVideoByCompanyId(companyId);

        CompanyNews companyNews = new CompanyNews();
        companyNews.setCompanyId(companyId);
        List<CompanyNews> companyNewss = companyNewsMapper.selectCompanyNewsList(companyNews);
        //宣传视频

        //查产品列表
        CompanyProduct companyProduct = new CompanyProduct();
        companyProduct.setCompanyId(company.getMemberId());
        List<CompanyProduct> companyProducts = companyProductMapper.selectCompanyProductList(companyProduct);
        //查环境图片列表
        CompanyEnvironment companyEnvironment = new CompanyEnvironment();
        companyEnvironment.setCompanyId(company.getMemberId());
        List<CompanyEnvironment> companyEnvironments = companyEnvironmentMapper.selectCompanyEnvironmentList(companyEnvironment);
        //查车间图片
        CompanyWorkshop companyWorkshop = new CompanyWorkshop();
        companyWorkshop.setCompanyId(companyId);
        List<CompanyWorkshop> companyWorkshops = companyWorkshopMapper.selectCompanyWorkshopList(companyWorkshop);


        //查个性签名
        CompanyAutograph companyAutograph = companyAutographMapper.selectCompanyAutographByCompanyId(companyId);

        // 企业简介
        if (info == null) {
            CompanyInfo companyInfo = new CompanyInfo();
            companyInfo.setCompanyId(company.getMemberId());
            companyInfo.setType(1);
            companyInfo.setIsVideo(1);
            companyInfo.setStyle(1);
            companyInfoService.insertCompanyInfo(companyInfo);
        }

        // 内容数
        if (company != null) {
            News news = new News();
            news.setMemberId(company.getMemberId());
            List<News> newsList = newsMapper.selectNewsList(news);
            data.put("contentNum",newsMapper.getAllNum(company.getMemberId()));// 内容数

            // 新闻获赞数
            int likesNum = 0;
            if (newsList.size() > 0) {
                for (News N : newsList) {
                    MemberNewsLikes memberNewsLikes = new MemberNewsLikes();
                    memberNewsLikes.setNewsId(N.getId());
                    List<MemberNewsLikes> newsLikesList = memberNewsLikesService.selectMemberNewsLikesList(memberNewsLikes);
                    likesNum = likesNum + newsLikesList.size();
                }
            }
            data.put("likesNum",likesNum);// 新闻获赞数

            // 关注数
            JiaqinFollowCompany followCompany = new JiaqinFollowCompany();
            followCompany.setMemberId(company.getMemberId());
            List<JiaqinFollowCompany> followCompanyList = jiaqinFollowCompanyService.selectJiaqinFollowCompanyList(followCompany);
            JiaqinFollowMember followMember = new JiaqinFollowMember();
            followMember.setMemberId(company.getMemberId());
            List<JiaqinFollowMember> followMemberList = jiaqinFollowMemberService.selectJiaqinFollowMemberList(followMember);
            data.put("followNum",followCompanyList.size() + followMemberList.size());// 关注数

            // 粉丝数
            JiaqinFollowMember fansMember = new JiaqinFollowMember();
            fansMember.setFollowMemberId(company.getMemberId());
            List<JiaqinFollowMember> fansMemberList = jiaqinFollowMemberService.selectJiaqinFollowMemberList(fansMember);
            data.put("fansNum",fansMemberList.size());// 粉丝数

            Member memberById = memberMapper.selectMemberById(company.getMemberId());
            if (memberById.getBusinessType() == 1) {
                data.put("business",memberById.getBusinessName());// 展示编辑主营业务
            } else {
                data.put("business",memberById.getBusiness());// 展示选择主营业务
            }

            data.put("synopsis",memberById.getSynopsis());// 主营介绍
            data.put("philosophy",memberById.getPhilosophy());// 经营理念
        } else {
            data.put("contentNum",0);// 内容数
        }

        data.put("companyCarousels",companyCarousels);
        data.put("companyNewss",companyNewss);
        data.put("companyProducts",companyProducts);
        data.put("companyEnvironments",companyEnvironments);
        data.put("info",info);
        data.put("company",company);
        data.put("companyWorkshops",companyWorkshops);
        data.put("companyVideo",companyVideo);
        data.put("companyAutograph",companyAutograph);
        return data;
    }

    /**
     * 查询公司列表
     *
     * @param company 公司
     * @return 公司
     */
    @Override
    public List<Company> selectCompanyList(Company company)
    {
        return companyMapper.selectCompanyList(company);
    }


    @Override
    public PageInfo<Company> selectCompanyListPage(Integer pageNum,Integer pageSize,String userId,Company company) {
        PageHelper.startPage(pageNum,pageSize);
        List<Company> list =  companyMapper.selectCompanyList(company);
        //list企业列表关联供应列表
        if(list != null && list.size()!=0){
            for (Company c:list) {
                //根据用户ID查询供应列表
                List<Supply> supplies =  supplyMapper.selectSupplyListByMemberId(c.getMemberId());
                for (Supply S: supplies) {
                    String[] split = S.getImgUrls().split(",");
                    if (split.length > 0) {
                        S.setImgUrls(split[0]);
                    }
                    c.setSupplies(supplies);
                }

                // 经营理念 主营业务
                Member memberById = memberMapper.selectMemberById(c.getMemberId());
                if (memberById != null ) {
                    // 展示编辑主营还是选择主营
                    if (memberById.getBusinessType() == 1) {
                        memberById.setBusiness(memberById.getBusinessName());
                    }
                    c.setMember(memberById);
                }
                JiaqinFollowCompany jiaqinFollowCompany = iJiaqinFollowCompanyService.selectJiaqinFollowMemberByMemberIdAndToCompanyId(userId,c.getId());
                if(jiaqinFollowCompany ==null){
                    c.setFollow(false);
                }else{
                    c.setFollow(true);
                }
                // 距离
//                Member member1 = memberMapper.selectMemberById(userId);
//                if (member1 != null) {
//                    double distance = DistanceUtils.getDistance(member1.getLongitude().doubleValue(), member1.getLatitude().doubleValue()
//                            , c.getLongitude().doubleValue(), c.getLatitude().doubleValue());
//                    DecimalFormat df = new DecimalFormat("######0.0");
//                    df.format(distance);
//                    c.setDistance(df.format(distance));
//                } else {
//                    c.setDistance("0.1");
//                }
                c.setMemberIdentity(memberMapper.selectMemberIdentityById(c.getMemberId()));
            }
        }
        PageInfo<Company> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增公司
     *
     * @param company 公司
     * @return 结果
     */
    @Override
    public int insertCompany(Company company)
    {
        company.setCreated(new Date());
        company.setUpdatedTime(new Date());
        company.setIsFamous(0);
        company.setCompanyNum(idGererateFactory.getUUID(5));
        return companyMapper.insertCompany(company);
    }

    /**
     * 修改公司
     *
     * @param company 公司
     * @return 结果
     */
    @Override
    public int updateCompany(Company company)
    {
        return companyMapper.updateCompany(company);
    }

    /**
     * 删除公司对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCompanyByIds(String ids)
    {
        return companyMapper.deleteCompanyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公司信息
     *
     * @param id 公司ID
     * @return 结果
     */
    @Override
    public int deleteCompanyById(String id)
    {
        return companyMapper.deleteCompanyById(id);
    }

    @Override
    public Company selectCompanyByCompanyNum(String companyNum) {
        return companyMapper.selectCompanyByCompanyNum(companyNum);
    }

    @Override
    public Company selectCompanyByCompanyPhone(String companyPhone) {
        return companyMapper.selectCompanyByCompanyPhone(companyPhone);
    }
}
